<?php
namespace app\techn_api_n\controller\v1;
use think\Db;
use app\service\MoneyCashService;
class Moneycash extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
	}

	/**
	 * 技师提现初始信息
	 */
	public function init()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		//查询技师提现收款账号信息
		$bind_bank = Db::name('bind_bank')->where(['identity_type' => 'techn', 'identity_id' => $params['techn']['id']])->find();
		//技师可提现金额
		$money = $params['techn']['money'];
		//最低提现金额
		$low_cash = 1;
		$result['code'] = 0;
	    $result['msg'] = "请求成功";
	    $result['data'] = [
	    	'bind_bank' => $bind_bank,
	    	'money' => $money,
	    	'low_cash' => $low_cash,
	    	'is_open_account' => 1
	    ];
	    echo json_encode($result);
	}

	/**
	 * 技师余额申请提现
	 */
	public function apply_cash()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'bind_bank_id' => 'require',
				'apply_money' => ['require', 'egt' => 1, 'regex' => '^([0-9]{1}\d{0,7})(\.\d{1,2})?$']
			],
			[
				'bind_bank_id.require' => '提现账号信息有误',
				'apply_money.require' => '请输入提现金额',
				'apply_money.egt' => '最低提现金额为1元',
				'apply_money.regex' => '提现金额格式有误'
			]
		);
		if(true !== $validate){
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		//查询提现账号是否存在
		$bind_bank_info = Db::name('bind_bank')->where(['id' => $params['bind_bank_id']])->find();
		if(empty($bind_bank_info))
		{
			echo json_encode(['code' => -1, 'msg' => '提现账户不存在']);
			exit();
		}
		$apply_money = PriceNumberFormat($params['apply_money']);
		if($apply_money > $params['techn']['money'])
		{
			echo json_encode(['code' => -1, 'msg' => '可提现金额不足']);
			exit();
		}
		//查询技师是否有要补单打订单
		$repair_order = Db::name('order')->where(['techn_id' => $params['techn']['id'], 'order_model' => 0, 'techn_is_repair' => 1, 'techn_repair_sid' => 0])->find();
		if(!empty($repair_order))
		{
			echo json_encode(['code' => -1, 'msg' => '您有未补单的订单请先处理']);
			exit();
		}



        $techn = Db::name('techn')->where('id',$params['techn']['id'])->find();
        if($techn['dongjie'] == 1)
        {
            echo json_encode(['code' => -1, 'msg' => '您的账户暂时不可提现']);
            exit();
        }
        $proxy = Db::name('proxy')->where('qu',$techn['county'])->find();
        if (empty($proxy)){
            $proxy = Db::name('proxy')->where('shi',$techn['city'])->find();
        }
		// 开始处理
		Db::startTrans();
		// 添加提现数据
		$money_cash_id = Db::name('money_cash')->insertGetId([
			'identity_type' => 'techn',
			'identity_id' => $params['techn']['id'],
			'cash_no' => date('YmdHis').GetRandomStr(6,'int'),
			'money' => $apply_money,
			'bank_name' => $bind_bank_info['bank_name'],
			'bank_accounts' => $bind_bank_info['bank_accounts'],
			'bank_username' => $bind_bank_info['bank_username'],
			'add_time' => time(),
			'upd_time' => time(),
			'form_proxy_id' => $proxy['proxy_id']
		]);
		if($money_cash_id <= 0)
		{
			//事务回滚
			Db::rollback();
			echo json_encode(['code' => -1, 'msg' => '提现申请失败']);
			exit();
		}
		//技师余额更新
		$techn_data = [
			'money' => Db::raw('money-'.$apply_money), //可用余额
			'lock_money' => Db::raw('lock_money+'.$apply_money)//锁定余额
		];
		if(!Db::name('techn')->where(['id' => $params['techn']['id']])->update($techn_data))
		{
			//事务回滚
			Db::rollback();
			echo json_encode(['code' => -1, 'msg' => '余额更新失败']);
			exit();
		}
		//日志
		$log_data = [
			[
	            'identity_type' => 'techn',
	            'identity_id' => $params['techn']['id'],
	            'business_type' => 6,
	            'money_type' => 0,
	            'operation_type' => 0,
	            'operation_money' => $apply_money,
	            'original_money' => $params['techn']['money'],
	            'latest_money' => $params['techn']['money']-$apply_money,
	            'msg' => '技师提现申请[有效金额减少'.$apply_money.'元]',
	            'add_time' => time()
	        ],
	        [
	            'identity_type' => 'techn',
	            'identity_id' => $params['techn']['id'],
	            'business_type' => 6,
	            'money_type' => 1,
	            'operation_type' => 1,
	            'operation_money' => $apply_money,
	            'original_money' => $params['techn']['lock_money'],
	            'latest_money' => $params['techn']['lock_money']+$apply_money,
	            'msg' => '技师提现申请[冻结金额增加'.$apply_money.'元]',
	            'add_time' => time()
	        ]
		];
		$money_log_count = Db::name('money_log')->insertAll($log_data);
		if($money_log_count < count($log_data))
		{
			//事务回滚
			Db::rollback();
			echo json_encode(['code' => -1, 'msg' => '额度日志添加失败']);
			exit();
		}
		// 提交事务
		Db::commit();
		echo json_encode(['code' => 0, 'msg' => '提现申请成功，等待管理员审核']);
	}

	/**
	 * 获取技师提现列表
	 */
	public function lists()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		//表单验证
		$validate = $this->validate(
			$params,
			[
				'page' => 'require'
			],
			[
				'page.require' => '分页有误'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        $map = [
        	'identity_type' => 'techn',
        	'identity_id' => $params['techn']['id'],
        ];
        $list_data = Db::name('money_cash')->where($map)->order('id desc')->limit(($params['page']-1)*20,20)->select();
        if(!empty($list_data))
        {
        	//打款状态
        	$status_array = MoneyCashService::$status_array;
        	//遍历处理
        	foreach($list_data as $key => $value)
        	{
        		//申请时间
        		$list_data[$key]['add_time_time'] = !empty($value['add_time']) ? date('m月d日 H:i', $value['add_time']) : '';
        		//处理状态
        		$list_data[$key]['status_name'] = !empty($status_array[$value['status']]['name']) ? $status_array[$value['status']]['name'] : '';
        	}
        	$result['code'] = 0;
	        $result['msg'] = "请求成功";
	        $result['data'] = $list_data;
        }else
        {
        	$result['code'] = -1;
	        $result['msg'] = "暂无数据";
        }
        echo json_encode($result);
	}
}